var form = layui.form,
    id=getQueryVariable("id");



var productCode=parent.code;
var specificationCode=parent.fjCode;

//货柜型号
let supplierList;
//获取供应商
supplier();
//当前时间
let nowTime=getNowFormatDate();
    //表单初始值，修改时调用
    if(id!=""){
        var oldData =  parent.table.cache["priceRangeTableId"];
        $.each(oldData, function (index, value) {
            if (oldData[index]["id"] == id) {
                form.val("editor-form",{
                    "productCode":oldData[index]["productCode"],
                    "specificationCode":oldData[index]["specificationCode"],
                    "supplyCode":oldData[index]["supplyCode"],
                    "supplyName":oldData[index]["supplyName"],
                    "minNum":oldData[index]["minNum"],
                    "maxNum":oldData[index]["maxNum"],
                    "taxIncludedFactory":oldData[index]["taxIncludedFactory"],
                    "unTaxIncludedFactory":oldData[index]["unTaxIncludedFactory"],
                    "taxIncludedFob":oldData[index]["taxIncludedFob"],
                    "unTaxIncludedFob":oldData[index]["unTaxIncludedFob"],
                    "createDate":oldData[index]["createDate"],
                    "updateDate":nowTime,
                })
            }
        });
        form.render();
    }else{
        form.val("editor-form",{
            "productCode":productCode,
            "specificationCode":specificationCode,
            "supplyCode":"",
            "supplyName":"",
            "minNum":0,
            "maxNum":0,
            "taxIncludedFactory":0,
            "unTaxIncludedFactory":0,
            "taxIncludedFob":0,
            "unTaxIncludedFob":0,
            "createDate":nowTime,
            "updateDate":null,
        })
        form.render();
    }
//监听提交
form.on('submit(form-element)', function(data){
    //data.field 为表单修改或新增数据 变量ID有值的情况下是修改，否则为新增
    if(id !="") data.field.id=id;
    $.ajax({
        type : 'post',
        data:JSON.stringify(data.field),
        url:'priceRange/savePriceRange',
        dataType:"json",
        contentType: 'application/json',
        async:false,
        success : function (da) {
            if(da.status==200 ){
                layer.msg("保存成功", {icon: 6});
                //保存成功关闭此页面
                var index = parent.layer.getFrameIndex(window.name);
                parent.layer.close(index);
                parent.refresh_price();
            }else {
                layer.msg('提交失败');
            }
        },
    });
    return false;
});
//监听供应商下拉框
form.on("select(supplyFilter)", function(data){
    if(data.value === ""){
        $('#supplyName').val("");
        return;
    }
    for(let i in supplierList){
        if(data.value == supplierList[i].code){
            $('#supplyName').val(supplierList[i].name);
        }
    }
});

//供应商
function supplier(){
    $.ajax({
        url: "customer2/getAllInspectUnit",
        async:false,
        success: function (res) {
            if (res.status == 200) {
                supplierList=res.data;
                for(let i in res.data){
                    $("#supplyCode").append(new Option(res.data[i].name, res.data[i].code));
                };
                form.render('select');
            } else {
                layer.msg(res.msg, {icon: 5});
            }
        }
    });
}

//时间处理 获取当前时间yyyy-MM-dd hh:mm:ss
function getNowFormatDate() {
    let date = new Date();
    let separator = "-";
    let separatorT=":";
    let year = date.getFullYear();
    let month = date.getMonth() + 1;
    let strDate = date.getDate();
    let hour=date.getHours();
    let minute=date.getMinutes();
    let second = date.getSeconds();

    if (month >= 1 && month <= 9) {
        month = "0" + month;
    }
    if (strDate >= 0 && strDate <= 9) {
        strDate = "0" + strDate;
    }
    if (hour >= 0 && hour <= 9) {
        hour = "0" + hour;
    }
    if (minute >= 0 && minute <= 9) {
        minute = "0" + minute;
    }
    if (second >= 0 && second <= 9) {
        second = "0" + second;
    }
    let currentDate = year + separator + month + separator + strDate+" "+hour+separatorT+minute+separatorT+second;
    return currentDate;

}
function isNumber(val) {
    var regPos = /^\d+(\.\d+)?$/; //非负浮点数
    var regNeg = /^(-(([0-9]+\.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*\.[0-9]+)|([0-9]*[1-9][0-9]*)))$/; //负浮点数
    if (regPos.test(val) || regNeg.test(val)) {
        return true;
    } else {
        return false;
    }
}

//计算未含税出厂价
function changeIncluded(){
    //含税出厂价
    let cgcb=$("#taxIncludedFactory").val();
    let cgcbBig
    if (isNumber(cgcb)){
        cgcbBig=new BigNumber(cgcb);
    }else{
        cgcbBig=new BigNumber(0);
    }
    let catalog=cgcbBig.dividedBy(1.13).toFixed(4);
    $("#unTaxIncludedFactory").val(catalog);
    form.render();
}

//计算未含税FOB价格
function changeIncludedFob(){
    //含税出厂价
    let fob=$("#taxIncludedFob").val();
    let fobBig
    if (isNumber(fob)){
        fobBig=new BigNumber(fob);
    }else{
        fobBig=new BigNumber(0);
    }
    let cshh=fobBig.dividedBy(1.13).toFixed(4);
    $("#unTaxIncludedFob").val(cshh);
    form.render();
}
